//
//  KFWNotificSystemToWebViewController.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/1/31.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//

#import "KFWNotificSystemToWebViewController.h"
#import "KFLRemoveSystemNotificationRequest.h"
@interface KFWNotificSystemToWebViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) NSString *string_url;
@property (nonatomic,strong) NSString *string_id;  //该条通知id

@property NSInteger unreadNum;
@property (nonatomic,strong) UIWebView *webview_systemweb;
@property (nonatomic,strong) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation KFWNotificSystemToWebViewController
@synthesize string_url;
@synthesize string_id;
@synthesize webview_systemweb;
@synthesize activityIndicatorView;
@synthesize unreadNum;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self ui_title:@"通知中心" withLeftImageName:@"button_back.png" withReftImageName:nil];
    [self.button_left addTarget:self action:@selector(systemToWebClickLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    webview_systemweb = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.view_title_super.frame.origin.y + self.view_title_super.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.view_title_super.frame.origin.y - self.view_title_super.frame.size.height)];
    webview_systemweb.scalesPageToFit =YES;
    webview_systemweb.dataDetectorTypes = UIDataDetectorTypeAll;
    webview_systemweb.delegate =self;
//    [webview_systemweb setBackgroundColor:[UIColor clearColor]];
//    webview_systemweb.opaque = NO;
    [self.view addSubview:webview_systemweb];
    
    activityIndicatorView = [[UIActivityIndicatorView alloc]
                             initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ;
    [activityIndicatorView setCenter: self.view.center] ;
    activityIndicatorView.color = RGB(91, 165, 229);
//    [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite] ;
    [self.view addSubview : activityIndicatorView] ;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

- (void)systemToWebClickLeftBtn:(id)sender
{
    [self popViewController];
}

- (id)initNotificSystemToWebWithUrlStr:(NSString *)urlstr andUnreadnum:(NSInteger)num andSystemNotificID:(NSString *)stringId
{
    self = [super init];
    if (self) {
        self.string_url = urlstr;
        self.unreadNum = num;
        self.string_id = stringId;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadWebview_systemwebWithString];
    if (self.unreadNum != 0) {
        KFLRemoveSystemNotificationRequest *request = [[KFLRemoveSystemNotificationRequest alloc] initRequest];
        request.n_id = self.string_id;
        [request startToRequestWithBlock:^(NSInteger stateCode, NSMutableArray *result) {
            switch (stateCode) {
                case 10000:
                {}
                    break;
                default:
                    break;
            }
        }];
    }
}

- (void)loadWebview_systemwebWithString
{
    NSURL *url =[NSURL URLWithString:string_url];
    NSLog(@"%@",string_url);
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webview_systemweb loadRequest:request];

}

//更新未读数为0

#pragma mark - UIWebViewDelegate -

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    [activityIndicatorView startAnimating];
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicatorView stopAnimating];
}
//页面出错
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    // CS 快法务 KLW 2014-12-30
    //    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"出错" message:[error localizedDescription]  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    //
    [activityIndicatorView stopAnimating];

    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"加载出错"  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    
    // CE 快法务 KLW 2014-12-30
    [alterview show];
}

@end
